

#include <linux/string.h>

#if defined(BUILD_UBOOT)
	#include <asm/arch/mt6577_gpio.h>
	#define LCM_PRINT printf
	#ifndef KERN_INFO
		#define KERN_INFO
	#endif
#else
	#include <linux/kernel.h>
	#include <mach/mt6577_gpio.h>
	#define LCM_PRINT printk
#endif

#if 1
#define LCM_DBG(fmt, arg...) \
	LCM_PRINT("[LCM-LT4015W-DSI] %s (line:%d) :" fmt "\r\n", __func__, __LINE__, ## arg)
#else
#define LCM_DBG(fmt, arg...) do {} while (0)
#endif

#include "lcm_drv.h"

#define LCM_DSI_CMD_MODE									1

// ---------------------------------------------------------------------------
//  Local Constants
// ---------------------------------------------------------------------------

#define FRAME_WIDTH  										(480)
#define FRAME_HEIGHT 										(800)

#define REGFLAG_DELAY             							0XFE
#define REGFLAG_END_OF_TABLE      							0xFA   // END OF REGISTERS MARKER

#define LCM_ID_OTM8009A		0x8009

// ---------------------------------------------------------------------------
//  Local Variables
// ---------------------------------------------------------------------------

static LCM_UTIL_FUNCS lcm_util = {0};

static int i = 0; //used for CE d400h, edit by Magnum 2012-7-17
unsigned  char mm[2] = {0x00,0x00};
//enable CE -- Color enhance edit by Magnum 2012-7-16
 //define par1 and par2.
unsigned  char par1[360] = { 	  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40} ;
 unsigned  char par2[360] = {  0x00,0x6a,0x00,0x69,0x00,0x68,0x00,0x67,0x00,0x66,0x00,0x66,
							  0x00,0x66,0x00,0x65,0x00,0x65,0x00,0x64,0x00,0x64,0x00,0x63,
							  0x00,0x63,0x00,0x63,0x00,0x62,0x00,0x62,0x00,0x61,0x00,0x61,
							  0x00,0x60,0x00,0x5d,0x00,0x5e,0x00,0x5f,0x00,0x60,0x00,0x61,
							  0x00,0x62,0x00,0x63,0x00,0x63,0x00,0x64,0x00,0x65,0x00,0x66,
							  0x00,0x67,0x00,0x69,0x00,0x6b,0x00,0x6c,0x00,0x6e,0x00,0x70,
							  0x00,0x71,0x00,0x73,0x00,0x75,0x00,0x76,0x00,0x78,0x00,0x7a,
							  0x00,0x7b,0x00,0x7d,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,0x00,0x7f,
							  0x00,0x7f,0x00,0x7e,0x00,0x7d,0x00,0x7c,0x00,0x7a,0x00,0x79,
							  0x00,0x78,0x00,0x77,0x00,0x75,0x00,0x74,0x00,0x73,0x00,0x72,
							  0x00,0x71,0x00,0x6f,0x00,0x6e,0x00,0x6d,0x00,0x6b,0x00,0x69,
							  0x00,0x68,0x00,0x66,0x00,0x64,0x00,0x63,0x00,0x61,0x00,0x5f,
							  0x00,0x5d,0x00,0x5c,0x00,0x5a,0x00,0x58,0x00,0x57,0x00,0x55};          
	 
	  
	  
	 


#define SET_RESET_PIN(v)    								(lcm_util.set_reset_pin((v)))

#define UDELAY(n) 											(lcm_util.udelay(n))
#define MDELAY(n) 											(lcm_util.mdelay(n))


// ---------------------------------------------------------------------------
//  Local Functions
// ---------------------------------------------------------------------------

#define dsi_set_cmdq_V2(cmd, count, ppara, force_update)	lcm_util.dsi_set_cmdq_V2(cmd, count, ppara, force_update)
#define dsi_set_cmdq(pdata, queue_size, force_update)		lcm_util.dsi_set_cmdq(pdata, queue_size, force_update)
#define wrtie_cmd(cmd)										lcm_util.dsi_write_cmd(cmd)
#define write_regs(addr, pdata, byte_nums)					lcm_util.dsi_write_regs(addr, pdata, byte_nums)
#define read_reg											lcm_util.dsi_read_reg()
#define read_reg_V2(cmd,buffer,buffer_size)					lcm_util.dsi_dcs_read_lcm_reg_v2(cmd,buffer,buffer_size)


struct LCM_setting_table {
    unsigned cmd;
    unsigned char count;
    unsigned char para_list[360];
};


static struct LCM_setting_table lcm_initialization_setting[] = {
	
	/*
	Note :

	Data ID will depends on the following rule.
	
		count of parameters > 1	=> Data ID = 0x39
		count of parameters = 1	=> Data ID = 0x15
		count of parameters = 0	=> Data ID = 0x05

	Structure Format :

	{DCS command, count of parameters, {parameter list}}
	{REGFLAG_DELAY, milliseconds of time, {}},

	...

	Setting ending by predefined flag
	
	{REGFLAG_END_OF_TABLE, 0x00, {}}
	*/


{0x00, 1 ,{0x00}}, 
{0xff, 3 ,{0x80,0x09,0x01}}, // enable EXTC

{0x00, 1 ,{0x80}}, 
{0xff, 2 ,{0x80,0x09}},      // enable Orise mode

{0x00, 1 ,{0x80}}, 
{0xf5, 12,{0x01,0x14,0x02,0x14,0x10,0x14,0x02,0x14,0x0e,0x14,0x0f,0x20}},
{REGFLAG_DELAY, 10, {}},

{0x00, 1 ,{0x90}}, 
{0xf5, 10,{0x02,0x14,0x08,0x14,0x06,0x14,0x0d,0x14,0x0b,0x14}},
{REGFLAG_DELAY, 10, {}},

{0x00, 1 ,{0xa0}}, 
{0xf5, 8 ,{0x10,0x14,0x01,0x14,0x14,0x14,0x14,0x14}},
{REGFLAG_DELAY, 10, {}},

{0x00, 1 ,{0xb0}}, 
{0xf5, 12,{0x14,0x14,0x12,0x14,0x13,0x14,0x11,0x14,0x13,0x14,0x00,0x00}},
{REGFLAG_DELAY, 10, {}},

{0x00 ,1 ,{0x80}}, 
{0xc4, 1 ,{0x30}}, // Source gnd
{REGFLAG_DELAY, 10, {}},

{0x00 ,1 ,{0x8A}}, 
{0xc4, 1 ,{0x40}}, // Source gnd
{REGFLAG_DELAY, 10, {}},

	
        // Column Inversion
	{0x00,	1,	{0xB4}}, 
    {0xc0,	1,	{0x10}},   //  50 lie rotation ,10 point rotation edit by Magnum 2012-8-22 
    {REGFLAG_DELAY, 10, {}},

	
	// pump45 01->X6X6 00->X8X6
	{0x00,	1,	{0x92}}, 
	{0xc5,	1,	{0x01}}, 
	{REGFLAG_DELAY, 10, {}},
	
	//pump1 X2 ratio
	{0x00,	1,	{0x90}}, 
	{0xc5,	1,	{0x96}}, 
	{REGFLAG_DELAY, 10, {}},
	
	//  pump34 CLK Line Rate 
	{0x00,	1,	{0x95}}, 
	{0xc5,	1,	{0x34}}, 
	{REGFLAG_DELAY, 10, {}},
	
	//  pump12 clk freq(Normal)
	{0x00,	1,	{0x94}},  
	{0xc5,	1,	{0x33}}, 
	{REGFLAG_DELAY, 10, {}},

	  // source pch
	{0x00,	1,	{0xa3}},
	{0xc0,	1,	{0x1b}}, 
	{REGFLAG_DELAY, 10, {}},
	
	//  REG-Pump23 AVEE VCL
	{0x00,	1,	{0x82}}, 
	{0xc5,	1,	{0x83}}, 
	{REGFLAG_DELAY, 10, {}},

	// source bias
	{0x00,	1,	{0x81}}, 
	{0xc4,	1,	{0x83}}, 
	{REGFLAG_DELAY, 10, {}},

	// VDD_18V=1.6V GVDD test on
	{0x00,	1,	{0xb1}},
	{0xc5,	1,	{0xa9}}, 
	{REGFLAG_DELAY, 10, {}},

	// [7:4]VGH level=12.5V, [3:0]VGL level=-13V  (setting value for worst pattern)
	{0x00,	1,	{0x91}},
	{0xc5,	1,	{0x2b}}, 
	{REGFLAG_DELAY, 10, {}},

	  // GVDD 4.625V
	{0x00,	1,	{0x00}}, 
	{0xd8,	1,	{0x79}}, 
	{REGFLAG_DELAY, 10, {}},

	 // NGVDD -4.625V
	{0x00,	1,	{0x01}}, 
	{0xd8,	1,	{0x79}}, 
	{REGFLAG_DELAY, 10, {}},

	  // Frame rate = 70Hz   
	{0x00,	1,	{0x81}}, 
	{0xc1,	1,	{0x66}}, 
	{REGFLAG_DELAY, 10, {}},

	 // VS, HS, DE for PORTB   
	{0x00,	1,	{0xa1}}, 
	{0xc1,	1,	{0x0e}}, 
	{REGFLAG_DELAY, 10, {}},

         // VCOM  
	{0x00,	1,	{0x00}},
	{0xd9,	1,	{0x3e}}, 
    {REGFLAG_DELAY, 10, {}},

//-----------------------------------------------------------------------------
//           gamma setting
//------------------------------------------------------------------------
//e10x
      
              {0x00,	1,	{0x00}}, 
              {0xe1,	16,	{0x00,0x0a,0x10,0x0d,0x06,0x10,0x0b,0x0a,0x03,0x06,0x0b,0x08,0x0f,0x12,0x0d,0x03}}, 
	          {REGFLAG_DELAY, 10, {}},

//e20x
         
              {0x00,	1,	{0x00}}, 
              {0xe2,	16,	{0x00,0x0a,0x10,0x0d,0x06,0x10,0x0b,0x0a,0x03,0x06,0x0b,0x08,0x0f,0x12,0x0d,0x03}}, 
	          {REGFLAG_DELAY, 10, {}},



 //--------------------------------------------------------------------------------
//		initial setting 2 < tcon_goa_wave >
//--------------------------------------------------------------------------------
													
	// CE8x : vst1, vst2, vst3, vst4
	// shift address
	{0x00,	1,	{0x80}}, 
	{0xce,	12,	{0x86,0x01,0x00,0x85,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}, 
	{REGFLAG_DELAY, 10, {}},


//CEAx : clka1, clka2
        // shift address
	{0x00,	1,	{0xa0}}, 
	{0xce,	14,	{0x18,0x04,0x03,0x21,0x00,0x00,0x00,0x18,0x03,0x03,0x22,0x00,0x00,0x00}}, 
	{REGFLAG_DELAY, 10, {}},


//CEBx : clka3, clka4
	 // shift address
	{0x00,	1,	{0xb0}},
	{0xce,	14,	{0x18,0x02,0x03,0x23,0x00,0x00,0x00,0x18,0x01,0x03,0x24,0x00,0x00,0x00}}, 
	{REGFLAG_DELAY, 10, {}},


//CFCx : 

	{0x00,	1,	{0xc0}}, 
	{0xcf,	10,	{0x01,0x01,0x20,0x20,0x00,0x00,0x01,0x00,0x00,0x00}}, 
	{REGFLAG_DELAY, 10, {}},

//CFDx : 
	// shift address
	
	
	{0x00,	1,	{0xd0}}, 
	{0xcf,	1,	{0x00}}, 
	{REGFLAG_DELAY, 10, {}},


//--------------------------------------------------------------------------------
//		initial setting 3 < Panel setting >
//--------------------------------------------------------------------------------


// cbcx
	 // shift address
	{0x00,	1,	{0xc0}}, 
	{0xcb,	15,	{0x00,0x14,0x14,0x14,0x14,0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}, 
	{REGFLAG_DELAY, 10, {}},


// cbdx  
	  // shift address
	{0x00,	1,	{0xd0}}, 
	{0xcb,	15,	{0x00,0x00,0x00,0x00,0x00,0x00,0x14,0x14,0x14,0x14,0x14,0x00,0x00,0x00,0x00}}, 

	{REGFLAG_DELAY, 10, {}},

	
// cbex
        // shift address
	{0x00,	1,	{0xe0}}, 
	{0xcb,	10,	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}, 
	{REGFLAG_DELAY, 10, {}},


	
// cc8x
       // shift address
	{0x00,	1,	{0x80}},
	{0xcc,	10,	{0x00,0x26,0x09,0x0b,0x01,0x25,0x00,0x00,0x00,0x00}}, 
	{REGFLAG_DELAY, 10, {}},

// cc9x
	// shift address

    {0x00,	1,	{0x90}}, 
	{0xcc,	15,	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x26,0x0a,0x0c,0x02}}, 
	{REGFLAG_DELAY, 10, {}},

// ccax
       // shift address

    {0x00,	1,	{0xa0}}, 
	{0xcc,	15,	{0x25,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}, 
	{REGFLAG_DELAY, 10, {}},

// ccbx
	  // shift address
	
    {0x00,	1,	{0xb0}},
	{0xcc,	10,	{0x00,0x25,0x0c,0x0a,0x02,0x26,0x00,0x00,0x00,0x00}}, 
	{REGFLAG_DELAY, 10, {}},

// cccx
	  // shift address
	
    {0x00,	1,	{0xc0}},
	{0xcc,	15,	{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x25,0x0b,0x09,0x01}}, 
	{REGFLAG_DELAY, 10, {}},

	//open TE  Magnum
	{0x35, 1, {0x00}}, //TE On 

	{0x00, 1, {0x00}},
	{0x44, 2, {0x01,0x22}},
	
//    {0x21, 1, {0x00}},   //Display off for Test Magnum 2012-7-16
  
	//open CE -- Color enhance Magnum 2012-7-16
/*	{0x00,1,{0x80}},
	{0xd6,1, {0x08}},

	{0x00, 1, {0x00}},

	
	
		{0xd4, 60, {			  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40}},
								  
		{0xd4, 60, {			  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40}},		
								  
		{0xd4, 60, {			  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40}},	
								  
		{0xd4, 60, {			  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40}},
								  
		{0xd4, 60, {			  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40}},	
								  
		{0xd4, 60, {			  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x40, 
								  0x00,0x40,0x00,0x40}},     // NO == 360 

	{0x00, 1, {0x00}},
	{0xd5, 60, {			0x00, 0x60, 0x00, 0x60, 0x00, 0x5f, 0x00, 0x5f, 0x00, 0x5e, 0x00, 0x5e, 0x00, 
							0x5d, 0x00, 0x5d, 0x00, 0x5d, 0x00, 0x5c, 0x00, 0x5c, 0x00, 0x5b, 0x00, 0x5b, 
							0x00, 0x5a, 0x00, 0x5a, 0x00, 0x5a, 0x00, 0x5b, 0x00, 0x5c, 0x00, 0x5d, 0x00, 
							0x5d, 0x00, 0x5e, 0x00, 0x5f, 0x00, 0x60, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63, 
							0x00, 0x63, 0x00, 0x64, 0x00, 0x65, 0x00, 0x66}},
							
	{0xd5, 60, {			0x00, 0x67, 0x00, 0x68, 0x00, 
							0x69, 0x00, 0x69, 0x00, 0x6a, 0x00, 0x6b, 0x00, 0x6c, 0x00, 0x6d, 0x00, 0x6e, 
							0x00, 0x6f, 0x00, 0x6f, 0x00, 0x70, 0x00, 0x71, 0x00, 0x72, 0x00, 0x73, 0x00, 
							0x74, 0x00, 0x74, 0x00, 0x75, 0x00, 0x76, 0x00, 0x77, 0x00, 0x78, 0x00, 0x78, 
							0x00, 0x79, 0x00, 0x7a, 0x00, 0x7b, 0x00, 0x7c, 0x00, 0x7d, 0x00, 0x7d, 0x00, 
							0x7e, 0x00, 0x7f}},
							
	{0xd5, 60, {			0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 
							0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 
							0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 
							0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 
							0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f}},

	{0xd5, 60, {			0x00, 0x7f, 
							0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 
							0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 
							0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 
							0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 
							0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f}},
							
	{0xd5, 60, {			0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 
							0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 
							0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 0x7f, 0x00, 
							0x7f, 0x00, 0x7e, 0x00, 0x7d, 0x00, 0x7c, 0x00, 0x7b, 0x00, 0x7a, 0x00, 0x7a, 
							0x00, 0x79, 0x00, 0x78, 0x00, 0x77, 0x00, 0x76, 0x00, 0x76, 0x00, 0x75, 0x00, 
							0x74}},
							
	{0xd5, 60, {			0x00, 0x73, 0x00, 0x72, 0x00, 0x71, 0x00, 0x71, 0x00, 0x70, 0x00, 0x6f, 
							0x00, 0x6e, 0x00, 0x6d, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6b, 0x00, 0x6a, 0x00, 
							0x69, 0x00, 0x68, 0x00, 0x67, 0x00, 0x66, 0x00, 0x66, 0x00, 0x66, 0x00, 0x65, 
							0x00, 0x65, 0x00, 0x64, 0x00, 0x64, 0x00, 0x63, 0x00, 0x63, 0x00, 0x63, 0x00, 
							0x62, 0x00, 0x62, 0x00, 0x61, 0x00, 0x61, 0x00, 0x60}},  */


	

// ccdx
	  // shift address
	
    {0x00,	1,	{0xd0}},
	{0xcc,	15,	{0x26,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}, 
    {REGFLAG_DELAY, 10, {}},
    
	{0x11,1,   {0x00}}, 
	{REGFLAG_DELAY, 120, {}},
	{0x29,1,	{0x00}}, 
    {REGFLAG_DELAY, 200, {}},

	
	//#VScan

	//(vodeo mode&command mode)
	//{0xB1,	2,	{0xF8,0x00}},
	//0512-87172138-7122, 15950021248
	//(only command mode)
//Ivan	{0xB1,	2,	{0xE8,0x00}},
//Ivan	{REGFLAG_DELAY, 1, {}},
	// Note
	// Strongly recommend not to set Sleep out / Display On here. That will cause messed frame to be shown as later the backlight is on.


	// Setting ending by predefined flag
	{REGFLAG_END_OF_TABLE, 0x00, {}}
};

static struct LCM_setting_table lcm_set_window[] = {
	{0x2A,	4,	{0x00, 0x00, (FRAME_WIDTH>>8), (FRAME_WIDTH&0xFF)}},
	{0x2B,	4,	{0x00, 0x00, (FRAME_HEIGHT>>8), (FRAME_HEIGHT&0xFF)}},
	{REGFLAG_END_OF_TABLE, 0x00, {}}
};


static struct LCM_setting_table lcm_sleep_out_setting[] = {
    // Sleep Out
	{0x11, 1, {0x00}},
	{REGFLAG_DELAY, 150, {}},

    // Display ON
	{0x29, 1, {0x00}},
	{REGFLAG_DELAY, 20, {}},
	{REGFLAG_END_OF_TABLE, 0x00, {}}
};

static struct LCM_setting_table lcm_compare_id_setting[] = {

	{0x00,	1,	{0x00}},
	{0xff,	3,	{0x80,0x09,0x01}}, 
	{REGFLAG_DELAY, 10, {}},

	{0x00,	1,	{0x80}},
	{0xff,	3,	{0x80,0x09,0x01}}, 
	{REGFLAG_DELAY, 10, {}},

	{0x00,	1,	{0x02}},

	{REGFLAG_END_OF_TABLE, 0x00, {}}


//	array[1]=0x010980ff; 
//	array[2]=0x80001500;
//	array[3]=0x00033902;
//	array[4]=0x010980ff;
};

static struct LCM_setting_table lcm_deep_sleep_mode_in_setting[] = {
	// Display off sequence
	{0x28, 1, {0x00}},

    // Sleep Mode On
	{0x10, 1, {0x00}},
	{REGFLAG_DELAY, 120, {}},

	{REGFLAG_END_OF_TABLE, 0x00, {}}
};

static void push_table(struct LCM_setting_table *table, unsigned int count, unsigned char force_update)
{
	unsigned int i;

	for(i = 0; i < count; i++) 
	{
		unsigned cmd;
		cmd = table[i].cmd;

		switch (cmd) {

		case REGFLAG_DELAY :
			MDELAY(table[i].count);
			break;

		case REGFLAG_END_OF_TABLE :
			break;

		default:
			dsi_set_cmdq_V2(cmd, table[i].count, table[i].para_list, force_update);
		}
    	}
	
}


// ---------------------------------------------------------------------------
//  LCM Driver Implementations
// ---------------------------------------------------------------------------

static void lcm_set_util_funcs(const LCM_UTIL_FUNCS *util)
{
    memcpy(&lcm_util, util, sizeof(LCM_UTIL_FUNCS));
}

//Ivan added
#define NS_TO_CYCLE(n, c)	((n) / c + (( (n) % c) ? 1 : 0))

static void lcm_get_params(LCM_PARAMS *params)
{
//	unsigned int cycle_time = (8 * 1000 * div2_real)/ (26 * (div1+0x01));
//	unsigned int ui = (1000 * div2_real)/ (26 * (div1+0x01)) + 1;
	unsigned int cycle_time = (8 * 1000 * 2)/ (26 * (26+0x01));		//~= 22.79ns
	unsigned int ui = (1000 * 2)/ (26 * (26+0x01)) + 1;			//~=3.84
    
	memset(params, 0, sizeof(LCM_PARAMS));
	
	params->type   = LCM_TYPE_DSI;

	params->width  = FRAME_WIDTH;
	params->height = FRAME_HEIGHT;

	// enable tearing-free
//	params->dbi.te_mode 				= LCM_DBI_TE_MODE_DISABLED;
//	params->dbi.te_edge_polarity		= LCM_POLARITY_RISING;

		// enable tearing-free 
       params->dbi.te_mode                 = LCM_DBI_TE_MODE_VSYNC_ONLY; 
       //params->dbi.te_mode                 = LCM_DBI_TE_MODE_VSYNC_OR_HSYNC; 
       params->dbi.te_edge_polarity        = LCM_POLARITY_FALLING; 

		
#if (LCM_DSI_CMD_MODE)
	params->dsi.mode   = CMD_MODE;
#else
	params->dsi.mode   = SYNC_PULSE_VDO_MODE;
#endif
	// DSI
	/* Command mode setting */
	params->dsi.LANE_NUM				= LCM_TWO_LANE;
	//The following defined the fomat for data coming from LCD engine.
	params->dsi.data_format.color_order = LCM_COLOR_ORDER_RGB;
	params->dsi.data_format.trans_seq   = LCM_DSI_TRANS_SEQ_MSB_FIRST;
	params->dsi.data_format.padding     = LCM_DSI_PADDING_ON_LSB;
	params->dsi.data_format.format      = LCM_DSI_FORMAT_RGB888;

	// Highly depends on LCD driver capability.
	params->dsi.packet_size=256;

	// Video mode setting		
	params->dsi.PS=LCM_PACKED_PS_24BIT_RGB888;

	params->dsi.word_count=480*3;	
	params->dsi.vertical_sync_active=2;
	params->dsi.vertical_backporch=2;
	params->dsi.vertical_frontporch=2;
	params->dsi.vertical_active_line=800;
		
#if (LCM_DSI_CMD_MODE)
	params->dsi.line_byte=2180;		// 2256 = 752*3
	params->dsi.horizontal_sync_active_byte=26;
	params->dsi.horizontal_backporch_byte=206;
	params->dsi.horizontal_frontporch_byte=206;	
	params->dsi.rgb_byte=(480*3+6);	

	params->dsi.horizontal_sync_active_word_count=20;	
	params->dsi.horizontal_backporch_word_count=200;
	params->dsi.horizontal_frontporch_word_count=200;

	//edit by Magnum 2012-7-4 try to solve lcd not updating....
   	params->dsi.HS_TRAIL=0x08; 
    params->dsi.HS_ZERO=0x07; 
    params->dsi.HS_PRPR=0x03; 
    params->dsi.LPX=0x04; 
        
    params->dsi.TA_SACK=0x01; 
    params->dsi.TA_GET=0x14; 
    params->dsi.TA_SURE=0x06;         
    params->dsi.TA_GO=0x10; 
        
    params->dsi.CLK_TRAIL=0x04; 
    params->dsi.CLK_ZERO=0x0f;         
    params->dsi.LPX_WAIT=0x01; 
    params->dsi.CONT_DET=0x00; 
        
    params->dsi.CLK_HS_PRPR=0x04; 

//Ivan added
//    params->dsi.HS_TRAIL=NS_TO_CYCLE(60 +4*ui + 10, cycle_time); 	//Page 272 
//    params->dsi.HS_ZERO=NS_TO_CYCLE(40 +4*ui + 10, cycle_time); 	//Page 272 
    params->dsi.HS_PRPR=NS_TO_CYCLE(105 +6*ui + 10, cycle_time); 	//Page 272
//    params->dsi.LPX=NS_TO_CYCLE(50 + 10, cycle_time); 	//Page 272
    
//    params->dsi.TA_SACK=0x01; 
//    params->dsi.TA_GET=NS_TO_CYCLE(6*58, cycle_time); 		//min 5*58
    params->dsi.TA_SURE=NS_TO_CYCLE(80, cycle_time);         	//min 58 ; max 2*58
//    params->dsi.TA_GO=NS_TO_CYCLE(5*58, cycle_time);     		//min 4*58
    
//    params->dsi.CLK_TRAIL=NS_TO_CYCLE(60 + 10, cycle_time); 	//min 60
//    params->dsi.CLK_ZERO=NS_TO_CYCLE(300, cycle_time);         	//min 300 -60
    params->dsi.CLK_HS_PRPR=NS_TO_CYCLE(60, cycle_time); 		//min 38 ; max 95
    
//    params->dsi.LPX_WAIT=0x01; 		//
//    params->dsi.CONT_DET=0x00;
    
    //params->dsi.pll_div1=30;                 
    //params->dsi.pll_div2=1;
	

#else
	// Video mode setting		
	params->dsi.intermediat_buffer_num = 2;

	params->dsi.horizontal_sync_active				= 10;
	params->dsi.horizontal_backporch				= 20;
	params->dsi.horizontal_frontporch				= 40;
	params->dsi.horizontal_active_pixel				= FRAME_WIDTH;
	
#endif
	// Bit rate calculation
	params->dsi.pll_div1=30;//34;		// fref=26MHz, fvco=fref*(div1+1)	(div1=0~63, fvco=500MHZ~1GHz)
	params->dsi.pll_div2=1;			// div2=0~15: fout=fvco/(2*div2)
//Ivan ~351MHz
}


static void lcm_init(void)
{
	LCM_DBG();    
	SET_RESET_PIN(1);
	MDELAY(10);
	SET_RESET_PIN(0);
	MDELAY(50);
	SET_RESET_PIN(1);
	MDELAY(100);

	push_table(lcm_initialization_setting, sizeof(lcm_initialization_setting) / sizeof(struct LCM_setting_table), 1);
}


static void lcm_suspend(void)
{
	LCM_DBG();
    
	push_table(lcm_deep_sleep_mode_in_setting, sizeof(lcm_deep_sleep_mode_in_setting) / sizeof(struct LCM_setting_table), 1);
}


static void lcm_resume(void)
{
	// Work around for Novatek driver IC. If it entered ULP mode, it must be reset before resume.
//	lcm_init();
	LCM_DBG();

	push_table(lcm_sleep_out_setting, sizeof(lcm_sleep_out_setting) / sizeof(struct LCM_setting_table), 1);

//	push_table(lcm_initialization_setting, sizeof(lcm_initialization_setting) / sizeof(struct LCM_setting_table), 1);
}


static void lcm_update(unsigned int x, unsigned int y,
                       unsigned int width, unsigned int height)
{
//Ivan    LCM_DBG("UPDATE-UPDATE-UPDATE");
	unsigned int x0 = x;
	unsigned int y0 = y;
	unsigned int x1 = x0 + width - 1;
	unsigned int y1 = y0 + height - 1;

	unsigned char x0_MSB = ((x0>>8)&0xFF);
	unsigned char x0_LSB = (x0&0xFF);
	unsigned char x1_MSB = ((x1>>8)&0xFF);
	unsigned char x1_LSB = (x1&0xFF);
	unsigned char y0_MSB = ((y0>>8)&0xFF);
	unsigned char y0_LSB = (y0&0xFF);
	unsigned char y1_MSB = ((y1>>8)&0xFF);
	unsigned char y1_LSB = (y1&0xFF);

	unsigned int data_array[16];

	data_array[0]= 0x00053902;
	data_array[1]= (x1_MSB<<24)|(x0_LSB<<16)|(x0_MSB<<8)|0x2a;
	data_array[2]= (x1_LSB);
	data_array[3]= 0x00053902;
	data_array[4]= (y1_MSB<<24)|(y0_LSB<<16)|(y0_MSB<<8)|0x2b;
	data_array[5]= (y1_LSB);
	data_array[6]= 0x002c3909;

	dsi_set_cmdq(&data_array, 7, 0);

}

static unsigned int lt4015w_cmp_id(void)
{
	unsigned int id=0;
	unsigned char buffer[2];
	unsigned int array[16];  


//reset before read id
	SET_RESET_PIN(1);
	SET_RESET_PIN(0);
	MDELAY(25);
	SET_RESET_PIN(1);
	MDELAY(50);

//
 /*   array[0]=0x00043902;
	array[1]=0x010980ff; 
	array[2]=0x80001500;
	array[3]=0x00033902;
	array[4]=0x010980ff;
	dsi_set_cmdq(array, 5, 1);
	MDELAY(10);

	

	array[0] = 0x02001500; //
	dsi_set_cmdq(array, 1, 1);  */
    push_table(lcm_compare_id_setting, sizeof(lcm_compare_id_setting) / sizeof(struct LCM_setting_table), 1);
	array[0] = 0x00023700;// set return byte number
	dsi_set_cmdq(array, 1, 1);

   
 //   MDELAY(10);
	
	read_reg_V2(0xD2, &buffer, 2);

	id = buffer[0]<<8 |buffer[1]; 

	LCM_DBG("[Magnum] -- otm8009a 0x%x , 0x%x , 0x%x \n",buffer[0],buffer[1],id);
		
	return (id == LCM_ID_OTM8009A)?1:0;

}

// ---------------------------------------------------------------------------
//  Get LCM Driver Hooks
// ---------------------------------------------------------------------------
LCM_DRIVER lt4015w_thai_dsi_lcm_drv = 
{
    .name			= "lt4015w_Thai",
	.set_util_funcs = lcm_set_util_funcs,
	.get_params     = lcm_get_params,
	.init           = lcm_init,
	.suspend        = lcm_suspend,
	.resume         = lcm_resume,
#if (LCM_DSI_CMD_MODE)
	.update         = lcm_update,
#endif
	.compare_id		= lt4015w_cmp_id,
};
